using System;
using Activa.CoolStorage;
using System.Text.RegularExpressions;

namespace Activa.ProMesh
{
	[MapTo("awtSession")]
	public abstract partial class Session : CSObject<Session, int> , ISessionRecord
	{
		public abstract int      SessionID       { get; }
		public abstract int      VisitorID       { get; set; }
		public abstract int?     UserID          { get; set; }
		public abstract string   HTTP_REFERER    { get; set; }
		public abstract string   REMOTE_ADDR     { get; set; }
		public abstract string   HTTP_USER_AGENT { get; set; }
		public abstract DateTime CreationTime    { get; }
		public abstract DateTime CreationDate    { get; }
		public abstract string   Browser         { get; set; }
		public abstract string   BrowserVersion  { get; set; }
		public abstract string   RefererPath { get; set; }
		public abstract string   RefererDomain { get; set; }
		public abstract string   RefererTLD { get; set; }
		public abstract string   RefererParameters { get; set; }
		public abstract bool?    RealSession { get; set; }

		[OneToMany] public abstract CSList<PageLogEntry> PageLogEntries { get; }
		[OneToMany]	public abstract CSList<TrafficEntry> TrafficEntries { get; }
		[ManyToOne]	public abstract Visitor                Visitor { get; set; }
	}

	public partial class Session
	{
		public void SetBrowserInfo()
		{
		}

		public void SetRefererInfo()
		{
			if (HTTP_REFERER == null || HTTP_REFERER.Trim().Length < 1)
				return;

			Regex regExp = new Regex(@"(?<url>(?<prot>http://)(?<referer>(?<path>(?<domain>(?<pretld>[a-z0-9\-\.]*\.)?(?<tld>[a-z\-0-9]+\.[a-z]+))(/[^?]*)?))(?<params>\?[^?]*)?)", RegexOptions.IgnoreCase | RegexOptions.Singleline);

			Match match = regExp.Match(HTTP_REFERER);

			if (match.Success)
			{
				RefererDomain = match.Groups["domain"].Value;
				RefererPath   = match.Groups["path"].Value;
				RefererTLD= match.Groups["tld"].Value;
				RefererParameters = match.Groups["params"].Value;
			}
		}
	}

}